<template>
    <div class="box">
        <div style="height: 6rm;"></div>
        <img style="width: 100%;" src="../../assets/image.png" alt="">
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="手机号" placeholder="手机号"
                    :rules="[{ pattern: telpattern, required: true, message: '请填写正确的手机号' }]" />
                <van-field v-model="password" :type="checked ? 'text' : 'password'" name="password" label="密码"
                    placeholder="密码" :rules="[{ pattern: pwdpattern, required: true, message: '请填写密码' }]">
                    <template #button>
                        <van-switch v-model="checked" />
                    </template>
                </van-field>
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { loginApi } from '@/api/api';
const checked = ref(false);
import { useCounterStore } from '@/stores/counter'
// 在组件内部的任何地方均可以访问变量 `store` ✨
const store = useCounterStore()
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
const route = useRoute()
const username = ref('');
const password = ref('');
const onSubmit = (values) => {
    console.log('submit', values);
    loginApi(username.value, password.value).then(res => {
        if (res.data.code == 0) {
            localStorage.setItem('token', res.data.data.token)
            store.userlist(res.data.data.username)
            localStorage.setItem('username', res.data.data.username)
            router.push('/home/my')
        }
    })
};

const telpattern = /^1[3-9]\d{9}$/;
const pwdpattern = /^\w{8}$/;
</script>

<style lang="scss" scoped></style>